import logging
from flask import abort
from flask import current_app
from flask import render_template

from info.models import News, Product
from info.module.index import index_blu


@index_blu.route('/')
def index():
    """首页展示"""
    news = None
    try:
        news = News.query.order_by(News.create_time.desc()).limit(5)
    except Exception as error:
        logging.debug(error)
        abort(403)


    if not news:
        abort(403)

    new_list = []

    for new in news:
        new_list.append(new.to_dict())



    products = None
    try:
        products = Product.query.all()
    except Exception as error:
        logging.debug(error)
        abort(403)

    if not products:
        abort(403)

    products_list = []
    for product in products:
        products_list.append(product.to_dict())
    data = {
        "new_list":new_list,
        "products_list": products_list

    }

    return render_template("index.html", data=data)